<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Http\Requests;

use DB;

use Intervention\Image\ImageManagerStatic as Image;

class CarouselController extends Controller
{
    //轮播图
    public function index(Request $request)
    {
        // 保存搜索的条件
        $where = [];
        $db = DB::table('storecarousel');

        if($request->has('cImg')){
            $db->where('cImg','like',"%{$request->input('cImg')}%");
            $where['cImg'] = $request->input('cImg');
        }
        $list = $db->paginate(2);
        
        // 原生语句
        return view('admin.Carousel',['list'=>$list,'where'=>$where]);
    }

    public function create()
    {
        return view('admin.carouselAdd');
    }

    public function store(Request $request)
    {      
        // 上传图片,获取除了指定字段，剩下的字段
        if($request->hasFile('cImg')){
                $file = $request->file('cImg');

                if ($file->isValid()) {
                    //获取后缀
                    $ext = $file->getClientOriginalExtension();
                    $picname = time().rand(1000,9999).'.'.$ext;
                    $file->move('./uploads/',$picname);
                    if($file->getError()>0){
                        echo '上传失败';
                    }else{
                        echo '上传成功';
                    }
                }
            }

        $data = $request->except('_token');
        $data['cImg'] = $picname;
        $data['cLinks'] = $picname;
        $row = DB::table('storecarousel')->insert($data);
        if($row>0){
            return redirect('/admin/Carousel')->with('msg','添加成功');
        }

    }

    public function edit($id)
    {
        $value = DB::table('storecarousel')->where('cId',$id)->first();
        return view('admin.carouselEdit',['v'=>$value]);
    }

    public function update($id,Request $request)
    {
        
        //获取指定字段
        // $data = $request->only('name','sex','age');
        // 获取除了指定字段，剩下的字段
        if($request->hasFile('cImg')){
                $file = $request->file('cImg');

                if ($file->isValid()) {
                    //获取后缀
                    $ext = $file->getClientOriginalExtension();
                    $picname = time().rand(1000,9999).'.'.$ext;
                    $file->move('./uploads/',$picname);
                    if($file->getError()>0){
                        echo '上传失败';
                    }else{
                        echo '上传成功';
                    } 
                }
            }

        $data = $request->except('_token','_method');
        $data['cImg'] = $picname;
        $data['cLinks'] = $picname;
        $row = DB::table('storecarousel')->where('cId',$id)->update($data);
        if($row>0){
            return redirect('/admin/Carousel')->with('msg','修改成功');
        }
    }

    public function destroy($cId)
    {

        $row = DB::table('storecarousel')->where('cId',$cId)->delete();
        if($row>0){
            return redirect('/admin/Carousel')->with('msg','删除成功');
        }
    }
}